import requests
import json
import os
import shutil
import zipfile


def zipDir(dirpath, outFullName):
    """
    压缩指定文件夹
    :param dirpath: 目标文件夹路径
    :param outFullName: 压缩文件保存路径+xxxx.zip
    :return: 无
    """
    zip = zipfile.ZipFile(outFullName, "w", zipfile.ZIP_DEFLATED)
    for path, dirnames, filenames in os.walk(dirpath):
        # 去掉目标跟路径，只对目标文件夹下边的文件及文件夹进行压缩
        fpath = path.replace(dirpath, '')

        for filename in filenames:
            zip.write(os.path.join(path, filename), os.path.join(fpath, filename))
    zip.close()


def run():
    dir_str = r'D:\work\androidProjects\qiudashi-android\app\build\outputs\apk'
    dir_base = r'D:\work\奇客创想'
    dir_new = r'球大师2.8.25'
    dir_all = os.path.join(dir_base, dir_new)
    if os.path.exists(dir_all):
        shutil.rmtree(dir_all)
    if not os.path.exists(dir_all):
        os.mkdir(dir_all)
    # 使用listdir
    dir_list = os.listdir(dir_str)
    for file in dir_list:
        print(file)
        for file2 in os.listdir(os.path.join(dir_str, file, 'release')):
            if file2.startswith("球大师"):
                print(file2)
                shutil.copy(os.path.join(dir_str, file, 'release', file2), dir_all)
    zipDir(dir_all, os.path.join(dir_base, f'{dir_new}.zip'))


if __name__ == '__main__':
    run()
